package com.spring.framgent;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.spring.activity.ChangeEmplInfoActivity;
import com.spring.activity.HelpActivity;
import com.spring.activity.LoginActivity;
import com.spring.activity.MainActivity;
import com.spring.activity.MyContactDataActivity;
import com.spring.activity.aboutActivity;
import com.spring.gowhere.R;
import com.spring.httputils.RequestParams;
import com.spring.httputils.RestClient;
import com.spring.httputils.TextHttpResponseHandler;
import com.spring.model.ContactData;
import com.spring.tool.App;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 更多服务
 * @author Administrator
 *
 */
public class Setting_Fragment extends Fragment {

	private Button btnselect, btnupdate, btnhelp, btnset, btnabout;
	RestClient restClient = null;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(final LayoutInflater inflater,
			ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.morefuwu, container, false);
		ImageButton imageButton = (ImageButton) view
				.findViewById(R.id.title_left_ib);
		btnupdate = (Button) view.findViewById(R.id.btnupdate);
		btnselect = (Button) view.findViewById(R.id.btnselect);

		btnhelp = (Button) view.findViewById(R.id.btnhelp);
		btnset = (Button) view.findViewById(R.id.btnset);

		btnabout = (Button) view.findViewById(R.id.btnabout);
		btnselect.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent(getActivity(),
						MyContactDataActivity.class);
				startActivity(intent);

			}
		});
		btnhelp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(getActivity(), "该模块正在开发中。。。", Toast.LENGTH_SHORT).show();
			}
		});
		btnset.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(),
						HelpActivity.class);
				startActivity(intent);
				
			}
		});

		btnabout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(),
						aboutActivity.class);
				startActivity(intent);
			}
		});
		btnupdate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// TODO Auto-generated method stub
				if (!(App.getInstance().isLogin())) {
					Toast.makeText(getActivity(), "请先登录", Toast.LENGTH_SHORT).show();
					Intent intent = new Intent();
					intent.setClass(getActivity(), LoginActivity.class);
					startActivity(intent);
				} else {
					Intent intent = new Intent(getActivity(),
							ChangeEmplInfoActivity.class);
					startActivity(intent);
				}
			}
		});
		btnselect = (Button) view.findViewById(R.id.btnselect);
		imageButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent Intent = new Intent(getActivity(), MainActivity.class);
				getActivity().startActivity(Intent);
			}
		});
		imageButton.setVisibility(View.GONE);
		TextView textView = (TextView) view.findViewById(R.id.title_tv);
		textView.setText("更多服务");
		return view;
	}

	private void SendHttpDate(String stredwebservicedaihao) {
		// String url =
		// "http://120.237.110.50:8097/andriodapp/WebService.asmx?op=EmployeeLogin";
		String url = App.getInstance().url + "/QueryContactList";
		RequestParams requestparams = new RequestParams(url, "POST");
		requestparams.put("ContactCaption", stredwebservicedaihao);
		requestparams.put("PageIndex", "1");

		TextHttpResponseHandler textHttpResponseHandler = new TextHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String responseString) {
				// TODO Auto-generated method stub
				Log.e("TAG", "responseString---" + responseString);
				if (responseString != null && !"".equals(responseString)) {
					AnalysisData(responseString);
				}

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				// TODO Auto-generated method stub
				Log.e("TAG", "responseString---" + responseString);
				// AnalysisData(responseString);

			}
		};
		try {
			restClient.post(url, requestparams, textHttpResponseHandler);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * json数据解析
	 * 
	 * @param strResult
	 * @throws JSONException
	 */
	private void AnalysisData(String strResult) {
		/**
		 * 这里需要分析服务器回传的json格式数据，
		 */
		JSONObject jsonObject = null;
		try {
			jsonObject = new JSONObject(strResult);
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			JSONArray jsonarray = jsonObject.getJSONArray("ContactList");
			for (int i = 0; i < jsonarray.length(); i++) {
				JSONObject json = jsonarray.getJSONObject(i);
				ContactData contactData = new ContactData();
				contactData.setContactCaption(json.getString("ContactCaption"));
				contactData.setAddr(json.getString("Addr"));
				contactData.setLinkMan(json.getString("LinkMan"));
				contactData.setLinkUrl(json.getString("LinkUrl"));
				contactData.setWebSrvUrl(json.getString("WebSrvUrl"));
				Log.e("sdf", "abcxxq---+------"+json.getString("LinkUrl:::") +"webSrvUrl:::"+json.getString("WebSrvUrl"));
//				App.getInstance().url = contactData.getWebSrvUrl();
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		getActivity().finish();
	}
}
